package com.wuage.infrastructure.security;

import com.wuage.application.IUacAccountService;
import com.wuage.domain.model.UacAccount;
import com.wuage.infrastructure.status.RoleEnum;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author Shynin
 * @version 1.0
 * @date 2020-09-03 00:25
 */
@Service
public class JwtUserDetailsServiceImpl implements UserDetailsService {

    @Resource
    private IUacAccountService accountService;

    @Override
    public UserDetails loadUserByUsername(String username) {
        UacAccount userByUsername = accountService.getUserByUsername(username);
        if(Objects.isNull(userByUsername) || !userByUsername.getEnable()) {
            return new JwtUser(0L, username, "", RoleEnum.CUSTOMER.getRoleCode(), true, 0L);
        }  else {
            return new JwtUser(userByUsername.getId(), userByUsername.getUsername(), userByUsername.getPassword(), userByUsername.getRole(), userByUsername.getEnable(), userByUsername.getTenantId());
        }
    }
}
